package com.ledim.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.EpisodesActivity;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;
import java.util.Iterator;

public class TVEpisodesAdapter extends LedimBaseListAdapter<LedimEpisodeBean> {

    private static final int POSITIVE = 180001;
    private static final int TV = 181031;
    public ArrayList<String> pList;
    public ArrayList<LedimEpisodeBean> pEpisodes;
    public boolean mAddPlay;
    private int mSelectedPosition = -1;
    public SparseBooleanArray pSelectionMap;
    public ArrayList<LedimEpisodeBean> pSelectedEpisodes;
    public int mPlayVid;
    private int mWidth;
    private EpisodesActivity.EpisodesInterface mEpisodesInterface;

    public TVEpisodesAdapter(Context context, ArrayList<String> list,
                             ArrayList<LedimEpisodeBean> episodes, boolean mAddPlay,
                             EpisodesActivity.EpisodesInterface mEpisodesInterface) {
        super(context);
        setDatas(episodes);
        this.pEpisodes = episodes;
        this.mAddPlay = mAddPlay;
        pSelectionMap = new SparseBooleanArray();
        pSelectedEpisodes = new ArrayList<LedimEpisodeBean>();
        pList = list;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mWidth = wm.getDefaultDisplay().getWidth();
        mWidth = (mWidth - DensityUtil.dip2px(context, 60)) / 5;
        this.mEpisodesInterface = mEpisodesInterface;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_episodes_gridview, null);
            holder.mEpisodeNum = (TextView) convertView.findViewById(R.id.item_episode_num);
            holder.mBg = convertView.findViewById(R.id.item_episode_bg);
            holder.mChoose = (ImageView) convertView.findViewById(R.id.item_episode_choose);
            ViewGroup.LayoutParams params = holder.mBg.getLayoutParams();
            params.height = mWidth;
            params.width = mWidth;
            holder.mBg.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //        final String num = pList.get(position);
        //        if (pList.size()==pEpisodes.size()){
        LedimEpisodeBean episode = getItem(position);
        if (!episode.videoType.equals("{}")) {
            switch (episode.getVedioType()) {
                case POSITIVE://正片
                case TV:
                    holder.mEpisodeNum.setText(episode.episode);
                    holder.mBg.setVisibility(View.VISIBLE);
                    break;
                default:
                    holder.mBg.setVisibility(View.GONE);
                    break;
            }
        }
        //        } else {
        //            holder.mEpisodeNum.setText(num);
        //            holder.mBg.setVisibility(View.VISIBLE);
        //        }

        if (mAddPlay) {
            listViewMulChoices(holder.mEpisodeNum, holder.mBg, position);
        } else {
            listViewSingleChoice(holder.mEpisodeNum, holder.mBg, holder.mChoose, position);
        }
        holder.mBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position < pEpisodes.size()) {
                    mPlayVid = pEpisodes.get(position).id;//当前播放的

                    if (mAddPlay) {
                        getSelectEpisodes(holder.mEpisodeNum, holder.mBg, position);
                    } else {
                        //刷新EpisodeView的选中剧集
                        setSelectItem(position);
                        notifyDataSetChanged();
                        mEpisodesInterface.onClickEpisode(mPlayVid);
                    }
                } else {
                    ToastUtil.toast("没有找到您请求的资源");
                }

            }
        });

        return convertView;
    }

    //多选
    private void getSelectEpisodes(TextView mNum, View mBg, int pos) {
        if (pSelectionMap.get(pos)) {
            pSelectionMap.put(pos, false);
            mNum.setTextColor(Color.parseColor("#666666"));
            mBg.setBackgroundResource(R.drawable.gray_box1);
            Iterator<LedimEpisodeBean> it = pSelectedEpisodes.iterator();
            while (it.hasNext()) {
                if (it.next() == pEpisodes.get(pos)) {
                    it.remove();
                }
            }
        } else {
            pSelectionMap.put(pos, true);
            mNum.setTextColor(Color.parseColor("#1a1a1a"));
            mBg.setBackgroundResource(R.drawable.check1x);
            pSelectedEpisodes.add(pEpisodes.get(pos));
        }
        int arg1;
        if (pSelectedEpisodes.size() > 0) {
            arg1 = 1;
        } else {
            arg1 = -1;
        }
        mEpisodesInterface.selectAllEpisodes(arg1, 0, pSelectedEpisodes);

    }

    private void listViewMulChoices(TextView mNum, View mBg, int sPosition) {
        if (pSelectionMap.get(sPosition)) {
            mNum.setTextColor(Color.parseColor("#1a1a1a"));
            mBg.setBackgroundResource(R.drawable.check1x);
        } else {
            mNum.setTextColor(Color.parseColor("#666666"));
            mBg.setBackgroundResource(R.drawable.gray_box1);
        }
    }

    //单选
    private void listViewSingleChoice(TextView mNum, View mBg, ImageView mChoose, int pos) {
        pSelectionMap.clear();
        pSelectedEpisodes.clear();
        if (mSelectedPosition == pos) {
            mNum.setTextColor(Color.parseColor("#1a1a1a"));
            mBg.setBackgroundResource(R.drawable.yellow_box1);
            mChoose.setVisibility(View.VISIBLE);
        } else {
            mNum.setTextColor(Color.parseColor("#666666"));
            mBg.setBackgroundResource(R.drawable.gray_box1);
            mChoose.setVisibility(View.GONE);
        }
    }

    public static class ViewHolder {

        private TextView mEpisodeNum;
        private View mBg;
        private ImageView mChoose;
    }

    //listview单选中的item
    public void setSelectItem(int position) {
        this.mSelectedPosition = position;
    }
}
